<?php
class News extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }

    public function index()
    {
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','title','required');
        $this->form_validation->set_rules('slug','slug','required');
        $this->form_validation->set_rules('text','text','required');


        if ($this->form_validation->run() == FALSE) {
            
            $this->load->view('news/myform');

        }else{

            $this->load->view('news/formsuccess');

        }

    }

    function create(){

        $this->load->helper('form');
        $this->load->library('form_validation');
        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }
        else
        {
            $this->news_model->set_news();
            $this->load->view('news/success');
        }

    }

    function testLike()
    {
        //$result = $this->db->like('title','混乱')->like('id',11)->get('news');
        //$result = $this->db->not_like('title','混搭')->get('news');

        $result = $this->db->not_like('title','混搭')->get('news');
        echo (json_encode($result->result()));  
    }

    function testWhere(){
        $arr = array('1','2','3');
        //where_in
        //or_where_in
        // $result = $this->db->where('id',10)->or_where_in('id',$arr)->get('news');
        $result = $this->db->or_where_in('id',$arr)->get('news');
        echo (json_encode($result->result()));
    }

    public function view($slug = NULL)
    {
        $data['news_item'] = $this->news_model->get_news($slug);
    }
    
    function querysql(){
        
        $query = $this->db->where('id > ','1')->get('news');
        echo  json_encode($query->result());
    }
    function getfrom()
    {
        $result = $this->db->selec('id,text')->from('news')->get();
        echo json_encode($result->result()) ;
    }

    function getmin(){
        //select_max(min,sum) 
       $result = $this->db->select_avg('id')->get('news');

       var_dump($result->result());

    }

    function addData()
    {
        $arr =  array(
            'title' => '史上第一混搭',
            'slug' => '额',
            'text' => '张小花',

        );
        for ($i=0; $i <6 ; $i++) { 

            $this->db->insert('news',$arr);
        }
    }

    function querytest1(){

         $query = $this->db->get('news');
           
            foreach ($query->result() as $row)
            {
                var_dump(json_encode($row));
                echo  '</br>';
            }

            var_dump(json_encode($query->result()));
    }
    function queryCount(){
        $result =  $this->db->count_all_results('id > ',1)->get('news');
        var_dump($result);
        return false;
    }
          
}